﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using Things.Common;
using Things.ThingsWeb;

namespace Things.ThingsWeb.Components
{
    public partial class GoodsCartTemp : System.Web.UI.UserControl
    {
        public decimal TotalCount
        {
            get
            {
                return GetCount();
            }
        }
        Things.BLL.BuyerAddress bll = new Things.BLL.BuyerAddress();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();

                string currentURL = Request.Url.ToString();
                if (currentURL.IndexOf("GoodsOrder.aspx") > 0)
                {
                    LinkButton2.Visible = false;
                    LinkButton3.Visible = false;
                }
                else
                {
                    LinkButton2.Visible = true;
                    LinkButton3.Visible = true;
                }

                GetCount();
            }
        }

        public void BindData()
        {
            List<Things.Model.GoodsCart> goodsCart = Session[ConstantString.SessionGoodsCart] as List<Things.Model.GoodsCart>;

            if (goodsCart == null || goodsCart.Count == 0)
            {
                LinkButton2.Visible = false;
                Panel1.Visible = true;
            }
            else
            {
                LinkButton2.Visible = true;
                Panel1.Visible = false;
            }

            gridView.DataSource = goodsCart;
            gridView.DataBind();

            
        }

        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridView.PageIndex = e.NewPageIndex;
            BindData();
        }

        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            e.Row.Attributes.Add("style", "background:#FFF");
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton linkbtnDel = (LinkButton)e.Row.FindControl("LinkButton1");
                linkbtnDel.Attributes.Add("onclick", "return confirm(\"你确认要删除吗\")");
            }
        }

        protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

            string ID = gridView.DataKeys[e.RowIndex].Value.ToString();
            Guid goodsPriceID = Guid.Empty;
            if (Guid.TryParse(ID, out goodsPriceID) == true)
            {
                PageUtility pu = new PageUtility();
                pu.Delete(goodsPriceID);
                BindData();
            }
        }

        protected void Test(object sender, EventArgs e)
        {
            GetCount();            
        }

        private decimal GetCount()
        {
            decimal totalCount = 0;

            List<Things.Model.GoodsCart> goodsCart = new List<Model.GoodsCart>();

            for (int i = 0; i < gridView.Rows.Count; i++)
            {
                Label lblGoodsPriceID = (Label)gridView.Rows[i].FindControl("lblGoodsPriceID");
                TextBox countValue = (TextBox)gridView.Rows[i].FindControl("txtGoodsCount");
                Label priceValue = (Label)gridView.Rows[i].FindControl("lblGoodsPrice");

                int count = 0;
                decimal price = 0;
                Guid gpID=Guid.Empty;
                if (int.TryParse(countValue.Text.Trim(), out count) == true 
                    && decimal.TryParse(priceValue.Text.Trim(), out price) == true
                    && Guid.TryParse(lblGoodsPriceID.Text.Trim(), out gpID) == true)
                {
                    totalCount += price * count;
                    goodsCart.Add(new Model.GoodsCart { GoodsPriceID = gpID, GoodsCount = count });
                }
                
            }

            Session[ConstantString.SessionGoodsCart] = goodsCart;

            this.lblCount.Text = totalCount.ToString();

            return totalCount;
        }

        private string GetSelIDlist()
        {
            string idlist = "";
            bool BxsChkd = false;
            for (int i = 0; i < gridView.Rows.Count; i++)
            {
                CheckBox ChkBxItem = (CheckBox)gridView.Rows[i].FindControl("DeleteThis");
                if (ChkBxItem != null && ChkBxItem.Checked)
                {
                    BxsChkd = true;
                    if (gridView.DataKeys[i].Value != null)
                    {
                        idlist += gridView.DataKeys[i].Value.ToString() + ",";
                    }
                }
            }
            if (BxsChkd)
            {
                idlist = idlist.Substring(0, idlist.LastIndexOf(","));
            }
            return idlist;
        }


        protected string GetGoodsName(string id)
        {            
            string temp = string.Empty;

            Things.Model.Goods goods = GetGoodsModel(id);
            if (goods != null)
            {
                temp = goods.GoodsName;
            }

            return temp;
        }

        protected string GetGoodsImageURL(string id)
        {
            string temp = string.Empty;

            Things.Model.Goods goods = GetGoodsModel(id);
            if (goods != null)
            {
                temp = goods.GoodsImageURL;
            }

            return temp;
        }

        protected string GetGoodsPrice(string id)
        {
            string temp = string.Empty;

            Guid goodsPriceID = Guid.Empty;
            if (Guid.TryParse(id, out goodsPriceID) == true)
            {
                Things.BLL.GoodsPrice gpBLL = new BLL.GoodsPrice();
                Things.Model.GoodsPrice gp = gpBLL.GetModel(goodsPriceID);

                if (gp != null)
                {
                    temp = gp.GoodsCurrentPrice.ToString();
                }
            }

            return temp;
        }

        private Things.Model.Goods GetGoodsModel(string id)
        {
            Things.Model.Goods temp = null;

            if (string.IsNullOrEmpty(id) == true)
            {
                return temp;
            }

            Guid goodsPriceID = Guid.Empty;
            if (Guid.TryParse(id, out goodsPriceID) == true)
            {
                Things.BLL.GoodsPrice gpBLL = new BLL.GoodsPrice();
                Things.Model.GoodsPrice gp = gpBLL.GetModel(goodsPriceID);

                if (gp != null)
                {
                    Things.BLL.Goods gBLL = new BLL.Goods();
                    Things.Model.Goods g = gBLL.GetModel(gp.GoodsID);

                    if (g != null)
                    {
                        temp = g;
                    }
                }
            }

            return temp;
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            Response.Redirect(ConstantString.GoodsOrderPageURL);
        }

        protected void LinkButton3_Click(object sender, EventArgs e)
        {
            Response.Redirect(ConstantString.HomePageURL);
        }



    }
}